package lpl.com.xyd.test.manage.controller;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.enums.ProjectStatusEnum;
import lpl.com.xyd.test.manage.common.response.HttpResponse;
import lpl.com.xyd.test.manage.common.utils.ContentType;
import lpl.com.xyd.test.manage.facade.ProjectFacade;
import lpl.com.xyd.test.manage.form.ProjectAddForm;
import lpl.com.xyd.test.manage.form.ProjectQueryForm;
import lpl.com.xyd.test.manage.info.ProjectInfo;
import lpl.com.xyd.test.manage.order.ProjectAddOrder;
import lpl.com.xyd.test.manage.order.ProjectQueryOrder;
import lpl.com.xyd.test.manage.order.ProjectUpdateOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {

    private final static Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private ProjectFacade projectFacade;

    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<ProjectInfo> add(@RequestBody ProjectAddForm form){
        LOGGER.info("新增项目, form:{}", JSONObject.toJSONString(form));
        ProjectAddOrder order = new ProjectAddOrder();
        BeanUtils.copyProperties(form,order);
        order.setStatus(ProjectStatusEnum.getByCode(form.getStatus()));
        return projectFacade.addProject(order);
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<List<ProjectInfo>> query(@RequestBody ProjectQueryForm form){
        LOGGER.info("查询项目, form:{}", JSONObject.toJSONString(form));
        ProjectQueryOrder order = new ProjectQueryOrder();
        BeanUtils.copyProperties(form,order);
        return projectFacade.queryList(order);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = {ContentType.APPLICATION_JSON_UTF_8,
            ContentType.TEXT_XML_UTF_8 })
    public HttpResponse<ProjectInfo> update(@RequestBody ProjectUpdateOrder form){
        LOGGER.info("修改项目, form:{}", JSONObject.toJSONString(form));
        ProjectUpdateOrder order = new ProjectUpdateOrder();
        BeanUtils.copyProperties(form,order);
        return projectFacade.update(order);
    }
}
